package net.godbirds.market.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.godbirds.bean.Code;
import net.godbirds.bean.Product;
import net.godbirds.bean.User;
import net.godbirds.market.service.MarketService;
import net.godbirds.market.service.WelcomeService;
/**
 * 欢迎首页控制器
 * @author godbi
 *
 */
@Controller
public class WelcomeController {

	@Autowired
	WelcomeService welcomeService;
	@Autowired
	MarketService marketService;
	/**
	 * 初始化进入首页，可根据使用记录推介产品
	 * @param request
	 * @return
	 */
    @GetMapping("/")
	public String index(HttpServletRequest request) {
    	User user = (User)request.getSession().getAttribute("user");
		List<Code> codes = welcomeService.getCodes("PRODUCT_GROUP");//类型
		//PRDTYPE=100( 特定产品) start=0 step=20
		List<Product> prds = marketService.getProductByGroup("100", 0, 20);
		request.setAttribute("navs", codes);//放入Session中
		request.setAttribute("user", user);//放入Session中
		request.setAttribute("prds",prds);
		request.getSession().setAttribute("user", user);
		return "index";
	}
    
    /**
     * 公共获取码值控制器
     * @param request
     * @param type
     * @return
     */
    @GetMapping("/code")
    @ResponseBody
	public List<Code> getCode(HttpServletRequest request,@RequestParam("type") String type) {
		//逻辑处理
		List<Code> codes = welcomeService.getCodes(type);//类型
		request.setAttribute("list",codes);
		return codes;
	}
}
